package com.trkj.service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
/**
 * 权限管理中的 员工管理
 * @author 吴山峰
 *
 */

import com.trkj.dao.DeptDAO;
import com.trkj.dao.EmpDAO;
import com.trkj.dao.MenuroleDAO;
import com.trkj.dao.PositionDAO;
import com.trkj.pojo.Emp;
import com.trkj.pojo.Menurole;
import com.trkj.pojo.Permission;
import com.trkj.utils.DataGrid;
import com.trkj.vo.EmpMenuRoleVo;
import com.trkj.vo.EmpRoleVo;
/**
 * 权限管理的员工管理service
 * @author 吴山峰
 *
 */
@Service
public class EmpRoleService{
	@Autowired
	EmpDAO empdDao;
	@Autowired
	MenuroleDAO menurole;
	@Autowired
	DeptDAO deptDao;
	@Autowired
	PositionDAO positionDao;
	@Autowired
	MenuroleDAO menuroleDao;
	
	/**, new Sort(Sort.Direction.fromStringOrNull(order), sort)
	 * 查询所有员工的角色||根据条件查询部分员工的角色
	 */
	
	public DataGrid pageSelect(String empName, Integer page, Integer rows, String sort, String order) {
		Pageable Pageable = new PageRequest(page-1, rows, new Sort(Sort.Direction.fromStringOrNull(order), sort));
		Page<Emp> resultPage = empdDao.findAll(new Specification<Emp>() {

			@Override
			public Predicate toPredicate(Root<Emp> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				// TODO Auto-generated method stub
				Path<String> name = root.get("empName");
				Predicate p1 = null;
				
				if ((empName != null &&empName.length() > 0)) {
					p1 = cb.like(name, empName + "%");
					return cb.and(p1);
				}else {
					return null;
				}
			}

		},Pageable);
		List<EmpRoleVo> list = empVolist(resultPage.getContent());
		/*Map<String, Object> map=new HashMap<>();
		map.put("total", list);
		map.put("rows", resultPage.getTotalElements());*/
		return new DataGrid(resultPage.getTotalElements(), list);
		
	}
	
	private List<EmpRoleVo> empVolist(List<Emp> list2) {
		// TODO Auto-generated method stub
		List<EmpRoleVo> list = new ArrayList<EmpRoleVo>();
		for(Emp emp:list2){
			list.add(empVo(emp));
		}
		return list;
	}
	
	
	private EmpRoleVo empVo(Emp emp) {
		EmpRoleVo vo = new EmpRoleVo();
		vo.setEmpName(emp.getEmpName());
		vo.setDeptName(emp.getDept().getDeptName());
		vo.setEmpSex(emp.getEmpSex());
		vo.setPositionName(emp.getPosition().getPositionName());
		vo.setEmpId(emp.getEmpId());
		List<String> listname = new ArrayList<>();
		List<Menurole> menuroles = emp.getMenuroles();
		for (Menurole menurole : menuroles) {
			listname.add(menurole.getRoleName());
		}
		vo.setMenuroles(listname);
		System.out.println(menuroles.size());
		return vo;
	}
	/**
	 * 根据员工id查询该员工所拥有的角色
	 * @return 
	 */
	public List<EmpMenuRoleVo> findEmpRoleById(Integer empid) {
		List<Object[]> findEmpRole = menuroleDao.findEmpRole(empid);
		List<EmpMenuRoleVo> listemp = new ArrayList<>();
		//将object转换成vo
		for (Object[] objects : findEmpRole) {
			EmpMenuRoleVo empRoleVo = new EmpMenuRoleVo();
			empRoleVo.setRoleid((int)objects[0]);
			empRoleVo.setRolename(objects[1]+"");
			empRoleVo.setChecked(objects[2]!=null);
			listemp.add(empRoleVo);
		}
		return listemp;
	}
	/**
	 * 给员工指定角色
	 */
	public boolean giveEmpRoleById(Integer empid, String roleids) {
		try {
		String [] arrroleids = null;
		if (roleids!=null && roleids.length()>0) {
			arrroleids = roleids.split(",");
		}
		
		Emp empone = empdDao.findOne(empid);
		//清空员工的角色，重新分配
		if(null == empone.getMenuroles()) {
			empone.setMenuroles(new ArrayList<>());
		}
		empone.getMenuroles().clear();
		//重新分配角色
		if(arrroleids != null) {
			for (int i = 0; i < arrroleids.length; i++) {
				empone.getMenuroles().add(menuroleDao.findByRoleId(Integer.parseInt(arrroleids[i])));
			}
		}
		empdDao.save(empone);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据员工id查询员工所拥有的全部功能id
	 */
	public Set<Integer> queryPermissionsIdByEmpId(Integer empid) {
		Emp emp = empdDao.findOne(empid);
		List<Menurole> menuroles = emp.getMenuroles();
		Set<Integer> perids = new HashSet<>();
		//循环遍历员工所拥有的角色并且遍历角色所拥有的功能id，加入到集合中
		for (Menurole menurole : menuroles) {
			List<Permission> permissions = menurole.getPermissions();
			for (Permission permission : permissions) {
				perids.add(permission.getPerId());
			}
		}
		return perids;
	}
}
